package de.hshn.mi.ajax;

import java.io.Serializable;
import javax.faces.event.ActionEvent;
import java.util.LinkedList;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.enterprise.context.SessionScoped;
import javax.inject.Named;

/**
 *
 * @author Shkelqim Turkaj
 */
@Named("itemManager")
@SessionScoped
public class ItemManagerBean implements Serializable {

    private List<String> upperItems;
    private List<String> lowerItems;
    private String[] upperSelected;
    private String[] lowerSelected;
    private String text;

    @PostConstruct
    private void init() {
        upperItems = new LinkedList<String>();
        lowerItems = new LinkedList<String>();
        fillList();
    }

    public void moveDown(ActionEvent event) {
        if (upperSelected != null) {
            for (String item : upperSelected) {
                lowerItems.add(item);
                upperItems.remove(item);
            }
        }
    }

    public void moveUp(ActionEvent event) {
        if (lowerSelected != null) {
            for (String item : lowerSelected) {
                upperItems.add(item);
                lowerItems.remove(item);
            }
        }
    }

    public void deleteSelection(ActionEvent event) {
        if (lowerSelected != null && !lowerItems.isEmpty()) {
            for (String item : lowerSelected) {
                lowerItems.remove(item);
            }
        }

        if (upperSelected != null) {
            for (String item1 : upperSelected) {
                upperItems.remove(item1);
            }

        }
    }

    public void fillList() {

        if (!upperItems.contains("Dim")) {
            upperItems.add("Dim");
        }
        if (!upperItems.contains("Jim")) {
            upperItems.add("Jim");
        }
        if (!upperItems.contains("Dita")) {
            upperItems.add("Dita");
        }
        if (!upperItems.contains("Baca")) {
            upperItems.add("Baca");
        }
        if (!lowerItems.contains("Taira")) {
            lowerItems.add("Taira");
        }
        if (!lowerItems.contains("Alina")) {
            lowerItems.add("Alina");
        }
        if (!lowerItems.contains("Kristina")) {
            lowerItems.add("Kristina");
        }
        if (!lowerItems.contains("Diana")) {
            lowerItems.add("Diana");
        }
        if (!lowerItems.contains("Victor")) {
            lowerItems.add("Victor");
        }
    }

    public void duplicateName() {
        if (upperSelected != null && !upperItems.isEmpty()) {
            for (String item1 : upperSelected) {
                upperItems.add(item1);
            }
        }
        if (lowerSelected != null && !lowerItems.isEmpty()) {
            for (String item : lowerSelected) {
                lowerItems.add(item);
            }
        }
    }

    public void cleanList() {
        upperItems.clear();
        lowerItems.clear();
    }

    public void pasteText(){
        if (text != null){
            upperItems.add(text);
        }
    }

    public String getText() {
        return text;
    }

    public void setText(String text) {
        assert !(text==null || text.isEmpty());
        this.text = text;
    }

    public String[] getUpperSelected() {
        return upperSelected;


    }

    public void setUpperSelected(String selected[]) {
        this.upperSelected = selected;


    }

    public String[] getLowerSelected() {
        return lowerSelected;


    }

    public void setLowerSelected(String selected[]) {
        this.lowerSelected = selected;


    }

    public List<String> getUpper() {
        return upperItems;


    }

    public List<String> getLower() {
        return lowerItems;

    }
}
